/*常用的三个变量*/
var projectTree = $.fn.zTree.init($("#projectTree"), pdm.setting, []); // 工程树
var mainTab = $(".main_panel").tab(); 	// 右边tab
var dialog = $().dialog();		  		// 页面对话框

pdm.loadTree(projectTree);

/*创建工程对话框
$("#add_project").click(function () {
    var form = $('<form class="dialog_form">\
					<label>工程名</label>&nbsp;&nbsp;<input type="text" oninput="var input=$(this);if(input.val()) {input.removeClass(\'not_null\')} else {input.addClass(\'not_null\')}"/>\
					<div class="dialog_buttons">\
						<button type="button">创建</button>\
						<button type="button" onclick="dialog.close();">取消</button>\
					</div>\
				</form>');
    dialog.show().setTitle("创建项目").setBody(form);

    form.find("button:first").click(function () {
        var btn = $(this);
        var input = btn.parent().prev();
        if (input.val()) {
            // 保存工程到后台 TODO
            //$.post('project/create', {"name": input.val()}, function (projectId) {
            // 实现后台代码后,从后台返回创建的ID,此处仅为了测试 TODO
            var projectId = commonTool.getTimeId();
            if (!isNaN(projectId)) {
                projectTree.addNodes(null, [{
                    name: input.val(),
                    id: projectId,
                    isParent: true,
                    type: "project",
                    projectId: projectId
                }]);
                dialog.close();
            } else {
                btn.html("创建失败");
                setTimeout(function () {
                    btn.html('创建');
                }, 500);
            }
            //});
        } else {
            input.addClass("not_null");
        }
    });
});
 */

/**
 * 添加模块/图
 */
$("#openWinAddModule").click(function () {
    var project = projectTree.getSelectedNodes()[0];
    pdmOpenWin.openWinAddOrEditModule(project, projectTree, true);
});

$("#openWinEditModule").click(function () {
    var project = projectTree.getSelectedNodes()[0];
    pdmOpenWin.openWinAddOrEditModule(project, projectTree, false);
});

$("#openWinAddVersion").click(function () {
    var project = projectTree.getSelectedNodes()[0];
    pdmOpenWin.openWinAddVersion(project, projectTree);
});

$("#delModule").click(function () {
    var project = projectTree.getSelectedNodes()[0];
    pdm.delModule(project, projectTree);
});


mainTab.panels.delegate(".deploy", "click", function () {
	pdm.deploy();
});
/**
 * 生成代码。生成代码的按钮所在的工具栏已经缓存了当前的画布对象了
 */
mainTab.panels.delegate(".generateCode", "click", function () {
    var moduleId = null;
    pdm.createCode(moduleId);
/*    var form = $('<form class="dialog_form">\
					<label>代码文件包名</label><input type="text" oninput="var input=$(this);if(input.val()) {input.removeClass(\'not_null\')} else {input.addClass(\'not_null\')}"/>\
					<div class="dialog_buttons">\
						<button type="button">确定</button>\
						<button type="button" onclick="dialog.close();">取消</button>\
					</div>\
				</form>');

    form.find("button:first").click(function () {
        var input = $(this).parent().prev();
        if (input.val()) {
            var canvas = dialog.canvas,
                project = dialog.project,
                chart = dialog.chart;

            var domainsChart = {};

            domainsChart.id = chart.id;
            domainsChart.name = chart.name;
            domainsChart.project = {};
            domainsChart.project.id = project.id;
            domainsChart.project.name = project.name;
            domainsChart.domainShapeDtos = canvas.getModels();
            domainsChart.domainShapeInfo = "";
            domainsChart.lineInfo = "";

            // 请求后台生成代码 TODO
            //$.ajax({
            //    headers: {
            //        'Content-Type': 'application/json'
            //    },
            //    url: "domains-chart/gencode?packageName=" + input.val(),
            //    data: JSON.stringify(domainsChart),
            //    type: "post",
            //    success: function (data) {
            //        window.location.assign(data + "/" + domainsChart.name + ".zip");
            //    },
            //    error: function () {
            //    }
            //});
        }
    });

    var toolBar = $(this).parents(".tools_bar:first");
    dialog.canvas = toolBar.data("canvas");
    dialog.project = toolBar.data("project");
    dialog.chart = toolBar.data("chart");

    dialog.show().setTitle("生成代码").setBody(form);*/



});

/**
 * 保存代码
 */
mainTab.panels.delegate(".saveUml", "click", function () {
    var toolBar = $(this).parents(".tools_bar:first"),
        canvas = toolBar.data("canvas"),
        domainsChart = {},
        chart = toolBar.data("chart"),
        project = toolBar.data("project");

    domainsChart.version = chart.version;
    domainsChart.id = chart.id;
    domainsChart.name = chart.name;
    domainsChart.project = {};
    domainsChart.project.id = project.id;
    domainsChart.project.name = project.name;

    domainsChart.lineInfo = canvas.getLineString(),
		domainsChart.domainShapeInfo = canvas.getModelString();

	pdm.saveCoordinates(canvas);

});

/**
 * 保存代码
 */
mainTab.panels.delegate(".testBtn", "click", function () {
    console.log('testBtn')

});
